package ch08_Swing高级组件.table;

import javax.swing.table.AbstractTableModel;
import java.util.Random;
import java.util.Vector;

//自定义表格数据模型类
public class StudentTableModel extends AbstractTableModel{


	@Override
	public int getRowCount() {		//得到表格总行数
		return rows.size();
	}

	@Override
	public int getColumnCount() {	//得到表格总列数
		return cols.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {	//得到指定行列的数据
		return ((Vector)rows.get(rowIndex)).get(columnIndex);
	}
	@Override
	public String getColumnName(int column) {		//得到指定列的名称
		return cols[column];
	}

	@Override
	public Class getColumnClass(int columnIndex) {	//得到指定列的类型
		return columnTypes[columnIndex];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {		//设置指定行列是否可编辑
		return columnIndex!=0&&columnIndex!=1;							//编号姓名列不可编辑
	}

	//表格行数据、表头、列名、列类型
	private Vector rows = new Vector();
	private Vector headers = new Vector();
	private String[] cols = {"编号","姓名","年龄","专业","是否为党员"};
	private Class[] columnTypes = {Integer.class,String.class,Integer.class,String.class,Boolean.class};

	//姓名列数据、专业列可选项
	private String[] names = {"王勇","张红","李明","刘晓亮","赵佳"};
	static String[] majors = {"软件工程","物联网工程","人工智能","大数据与云计算"};
	//默认构造方法
	StudentTableModel(){
		this.initTableHeaders();
		this.initTableRows();
	}
	//初始化表头
	void initTableHeaders() {
		for(String c:cols) {
			headers.add(c);
		}
	}
	//初始化表格行数据
	void initTableRows() {
		Random rand = new Random();
		for (int i = 0; i < names.length; i++) {
			Vector row = new Vector();  //构造行向量
			row.add(i+1);				//编号
			row.add(names[i]);			//姓名
			row.add(rand.nextInt(8)+18);//随机产生年龄
			row.add(majors[rand.nextInt(majors.length)]);//随机选择专业
			row.add(rand.nextBoolean());	//随机产生true或false
			rows.add(row); 					//加入表格行数据
		}
	}


}
